import moment from 'moment'
let clickEventClose = ''
export default {
  errorimg: {
    inserted(dom, obj) {
      // 判断dom是否有src 没有就添加固定的
      dom.src = dom.src || obj.value || require('@/assets/common/head.jpg')
      dom.onerror = () => {
        dom.src = obj.value || require('@/assets/common/head.jpg')
      }
    },
    componentUpdated(dom, obj) {
      dom.src = dom.src || obj.value || require('@/assets/common/head.jpg')
    }
  },
  // 点击dom外 dom关闭
  clickEventClose: {
    inserted(dom, obj) {
      clickEventClose = (e) => {
        if (!dom.contains(e.target)) {
          obj.value()
        }
      }
      document.addEventListener('click', clickEventClose)
    },
    unbind() {
      document.removeEventListener('click', clickEventClose)
    }
  },
  // 使用自定义指令转化时间
  formatTime: {
    inserted(dom, obj) {
      dom.innerHTML = moment(obj.value).format('YYYY-MM-DD')
    },
    componentUpdated(dom, obj) {
      dom.innerHTML = moment(obj.value).format('YYYY-MM-DD')
    }
  }
}
